package cn.Mmovie.dao.Impl;

import cn.Mmovie.bean.Auditorium;
import cn.Mmovie.bean.Cinema;
import cn.Mmovie.bean.Movie;
import cn.Mmovie.bean.Schedule;
import cn.Mmovie.dao.MovieDao;
import cn.Mmovie.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MovieDaoImpl implements MovieDao {
    @Override
    public Integer insert(Movie movie) {
        return null;
    }

    @Override
    public Integer delete(Integer id) {
        return null;
    }

    @Override
    public Integer update(Movie movie) {
        return null;
    }

    @Override
    public Movie selectById(Integer id) {
        return null;
    }

    @Override
    public List<Movie> selectAll() {
        List<Movie> movies = null;
        try {
            Connection conn = DbUtil.getConnection();
            String sql = "SELECT * FROM movie";
            PreparedStatement psmt = conn.prepareStatement(sql);

            ResultSet rs = psmt.executeQuery();
            movies = new ArrayList<>();
            while (rs.next()) {
                Movie movie=new Movie();
                movie.setId(rs.getInt("id"));
                movie.setName(rs.getString("name"));
                movie.setAuditorium(new Auditorium(rs.getInt("auditorium")));
                movie.setDirector(rs.getString("director"));
                movie.setPicture(rs.getString("picture"));
                movies.add(movie);
            }
            DbUtil.close(rs, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return movies;
    }

    @Override
    public List<Movie> selectForPageItems(Integer offset, Integer rows) {
        List<Movie> movies = null;
        try {
            Connection conn = DbUtil.getConnection();
            String sql ="SELECT m.id mid,m.name mname,m.date mdate,m.director mdirector," +
                    "m.picture mpicture,m.type mtype,m.origin morigin,m.main_actor mmain_actor," +
                    "m.access maccess,m.introduce mintroduce,a.id aid,a.name aname FROM movie m LEFT JOIN auditorium a ON m.auditorium=a.id ORDER BY m.id DESC LIMIT ?,?";
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setInt(1, offset);
            psmt.setInt(2, rows);
            ResultSet rs = psmt.executeQuery();
            movies = new ArrayList<>();
            while (rs.next()) {
                Movie movie=new Movie();
                movie.setName(rs.getString("mname"));
                movie.setDate(rs.getDate("mdate"));
                movie.setAuditorium(new Auditorium(rs.getInt("aid"),rs.getString("aname")));
                movie.setDirector(rs.getString("mdirector"));
                movie.setPicture(rs.getString("mpicture"));
                movie.setType(rs.getString("mtype"));
                movie.setOrigin(rs.getString("morigin"));
                movie.setMain_actor(rs.getString("mmain_actor"));
                movie.setAccess(rs.getDouble("maccess"));
                movie.setIntroduce(rs.getString("mintroduce"));
                movies.add(movie);
            }
            DbUtil.close(rs, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return movies;
    }

    @Override
    public Integer selectForRecordTotal() {
        Integer result = null;
        try {
            Connection conn = DbUtil.getConnection();
            String sql = "SELECT COUNT(*) FROM movie ORDER BY id DESC";
            PreparedStatement psmt = conn.prepareStatement(sql);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                result = rs.getInt(1);
            }
            DbUtil.close(null, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
}
